package cn.flyingocean.spider.impl.com.douban.book;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.commons.logging.LogFactory;

import java.util.logging.Level;

/**
 * 这个抽象Spider提供一个HtmlUnit实现的WebClient
 */
public abstract class WebClientSpider extends FileHandlerSpider{
    private WebClient webClient;

    /**
     * 惰性加载 WebClient
     * @return
     */
    public WebClient getWebClient(){
        if (webClient==null){
            // 屏蔽HtmlUnit等系统 log
            LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
            java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
            java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);

            // HtmlUnit 模拟浏览器
            WebClient webClient = new WebClient(BrowserVersion.CHROME);
            webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器，默认为true
            webClient.getOptions().setCssEnabled(false);                    // 禁用css支持
            webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时，是否抛出异常
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间
            webClient.waitForBackgroundJavaScript(30 * 1000);               // 等待js后台执行30秒
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要，设置支持AJAX

            this.webClient = webClient;
            return webClient;
        }else{
            return webClient;
        }

    }

    /**
     * 关闭WebClient
     */
    @Override
    public void beforeDead() {
        webClient.close();
        super.beforeDead();
    }
}
